/**
 * 项目初始化
 *
 * @author liuqi
 * @date 2024/12/20 20:55
 **/
import setter from "./setter";
import tools from "../util/Tools";
import axios from "axios";
import AxiosRequest from "../util/AxiosRequest";

/**
 * 设置浏览器标题和favicon
 */
function setProjectTitleAndFavicon() {
    document.title = setter.project.title;
    let link = document.querySelector("link[rel*='icon']") || document.createElement("link");
    if (!link.rel) {
        link.type = "image/x-icon";
        link.rel = "icon";
    }
    link.href = setter.project.favicon;
    document.getElementsByTagName("head")[0].appendChild(link);
}

export default {
    async initialization() {
        // 加载 plugin/config, 初始化项目配置参数
        let res = await axios.get("./config.json");
        tools.merge(setter.project, res.data.project);
        tools.merge(setter.server, res.data.server);
        setter.agentChatURL = res.data.agentChatURL;

        if (setter.server.request.baseURL) {
            let baseURL = setter.server.request.baseURL;
            if (baseURL.endsWith("/")) {
                baseURL = baseURL.substring(0, baseURL.length - 1);
            }
            setter.server.request.baseURL = baseURL;
        }

        // 设置浏览器标题以及favicon
        setProjectTitleAndFavicon();

        // 初始化 axios请求
        AxiosRequest.init();
    }
}
